const constant = require("../../util/constant.js");
const util = require('../../util/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userid: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.data.userid = wx.getStorageSync("userid")
        //console.log(this.data.userid)
        //var uid = wx.getStorageSync("userid");
        if (this.data.userid == '') {
          wx.redirectTo({
            url: '/pages/login/index',
          })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    handleSubmit: function (event) {

        var old_pass = event.detail.value.old_pass;
        var link_pass = event.detail.value.link_pass;
        var check_pass = event.detail.value.check_pass;
        var uid=this.data.userid;
        //console.log(uid+"|"+old_pass+"|"+link_pass)
        if (old_pass == '') {
          util.showFailToast({
            title: '请输入原密码'
          });
    
          return;
        } 
    
        if (link_pass == '') {
          util.showFailToast({
            title: '请输入新密码'
          });
    
          return;
        }
        if (check_pass == '') {
          util.showFailToast({
            title: '请输入确认密码'
          });
    
          return;
        }
        if (link_pass != check_pass) {
          util.showFailToast({
            title: '密码不一致'
          });
    
          return;
        }
    
       
        var that = this;
        wx.request({
          url: constant.host+'/appserv/uppass.ashx',
          header: {
            //'Content-type':'application/x-www-form-urlencoded'
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: "POST",
          data: util.json2Form({
            cid:uid,
            op: old_pass,
            np: link_pass,
            k: constant.wkey
          }),
          success: function (res) {
            if (res.statusCode == 200) { 
              if (res.data == '0') {
                util.showFailToast({
                  title: '原密码不正确'
                });
                return;
              }
              else
              {
                  setTimeout(function(){
                    wx.showToast({
                      title: '密码修改成功',
                    }),
                    500
                  });
                  wx.navigateBack();
              }
         
            }
            else {
              util.showFailToast(
                {
                  title: "修改失败请重试"
                }
              )
            }
          },
        });
      }
})